<?php

namespace Core\Base;

/**
 * Description of Widget
 *
 * @author Admin
 */
class Widget
{
	protected $_vars = array();
	protected $_configs = array();
	
	/**
	 *
	 * @var \Core\Layout
	 */
	protected $_layout;
	
	public function setVars($vars)
	{
		$this->_vars = $vars;
	}
	
	public function setConfigs($configs, $layout = null)
	{
		$this->_configs = $configs;
		
		$this->_layout = $layout;
	}
	
	public function assign($key, $val)
	{
		$this->_vars[$key] = $val;
	}
	
	public function __get($key)
	{
		if(!isset($this->_vars[$key]))
			throw new \Core\Exception('Not defined variable:'.((string)$key));
		
		return $this->_vars[$key];
	}
	
	public function __call($func, $args)
	{
		return call_user_func_array(array($this->_layout, $func), $args);
	}
	
	public function fetch($file)
	{
		$path = $file . $this->_configs['ext'];
		if(!file_exists($path))
			$path = rtrim($this->_configs['widgetsPath'], '/') .'/'. ltrim($path, '/');

		ob_start();
		
		require $path;
		
		return ob_get_clean();
	}
}
